package com.cookor.poi.modules.mx.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.utils.excel.ExportExcel;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.mx.entity.MxSfj;
import com.cookor.poi.modules.mx.service.MxSfjService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/mx/mxSfj"})
public class MxSfjController extends BaseController {
  @Autowired
  private MxSfjService mxSfjService;
  
  @ModelAttribute
  public MxSfj get(@RequestParam(required = false) String id) {
    MxSfj entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.mxSfjService.get(id); 
    if (entity == null)
      entity = new MxSfj(); 
    return entity;
  }
  
  @RequiresPermissions({"mx:mxSfj:view"})
  @RequestMapping({"list", ""})
  public String list(MxSfj mxSfj, HttpServletRequest request, HttpServletResponse response, Model model) {
    Page<MxSfj> page = this.mxSfjService.findPage(new Page(request, response), mxSfj);
    model.addAttribute("page", page);
    return "modules/mx/mxSfjList";
  }
  
  @RequiresPermissions({"mx:mxSfj:view"})
  @RequestMapping({"form"})
  public String form(MxSfj mxSfj, Model model) {
    model.addAttribute("mxSfj", mxSfj);
    return "modules/mx/mxSfjForm";
  }
  
  @RequiresPermissions({"mx:mxSfj:edit"})
  @RequestMapping({"save"})
  public String save(MxSfj mxSfj, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, mxSfj, new Class[0]))
      return form(mxSfj, model); 
    this.mxSfjService.save(mxSfj);
    addMessage(redirectAttributes, new String[] { "保存美信三伏灸订单成功" });
    return "redirect:" + Global.getAdminPath() + "/mx/mxSfj/?repage";
  }
  
  @RequiresPermissions({"mx:mxSfj:edit"})
  @RequestMapping({"delete"})
  public String delete(MxSfj mxSfj, RedirectAttributes redirectAttributes) {
    this.mxSfjService.delete(mxSfj);
    addMessage(redirectAttributes, new String[] { "删除美信三伏灸订单成功" });
    return "redirect:" + Global.getAdminPath() + "/mx/mxSfj/?repage";
  }
  
  @RequestMapping({"updateStatus"})
  public String updateStatus(String id, String status, RedirectAttributes redirectAttributes) {
    MxSfj mxSfj = this.mxSfjService.get(id);
    mxSfj.setStatus(status);
    this.mxSfjService.save(mxSfj);
    addMessage(redirectAttributes, new String[] { "修改成功" });
    return "redirect:" + Global.getAdminPath() + "/mx/mxSfj/?repage";
  }
  
  @RequestMapping(value = {"export"}, method = {RequestMethod.POST})
  public String exportFile(MxSfj mxSfj, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
    try {
      String fileName = "预约数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
      Page<MxSfj> page = this.mxSfjService.findPage(new Page(request, response), mxSfj);
      for (MxSfj bean : page.getList()) {
        bean.setStatus("1");
        this.mxSfjService.save(bean);
      } 
      (new ExportExcel("预约数据", MxSfj.class)).setDataList(page.getList()).write(response, fileName).dispose();
      return null;
    } catch (Exception e) {
      e.printStackTrace();
      addMessage(redirectAttributes, new String[] { "导出预约数据失败！失败信息：" + e.getMessage() });
      return "redirect:" + this.adminPath + "/mx/mxSfj/?repage";
    } 
  }
  
  @RequestMapping(value = {"exportV2"}, method = {RequestMethod.POST})
  public String exportFileV2(MxSfj mxSfj, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
    try {
      String fileName = "预约数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
      Page<MxSfj> page = this.mxSfjService.findPage(new Page(request, response), mxSfj);
      List<MxSfj> list = page.getList();
      for (MxSfj bean : list) {
        bean.setStatus("1");
        this.mxSfjService.save(bean);
        bean.setMobile(bean.getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
      } 
      (new ExportExcel("预约数据", MxSfj.class)).setDataList(list).write(response, fileName).dispose();
      return null;
    } catch (Exception e) {
      e.printStackTrace();
      addMessage(redirectAttributes, new String[] { "导出预约数据失败！失败信息：" + e.getMessage() });
      return "redirect:" + this.adminPath + "/mx/mxSfj/?repage";
    } 
  }

}
